package com.flyee.viewer.pack.helper
{
	import com.flyee.utils.MyLog;
	
	import flash.display.BitmapData;
	
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ObjectProxy;
	
	/**
	 * 道具的一些逻辑处理类 
	 * @author dfengwji
	 * 
	 */	
	public class ItemHelper
	{
		//private var itemService:ItemFlexService = null;
		//private var packageService:UserPackageFlexService = null;
		private var userID:String = null;
		private var itemsContainer:ItemsContainer = null;
		public function ItemHelper(root:ItemsContainer)
		{
			itemsContainer = root;
			//userID = ApplicationContext.getInstance().sessionUser.id;
			//itemService = new ItemFlexService();
			//itemService.addEventListener(RemoteExceptionEvent.REMOTE_EXCEPTION,exceptionEvent);
			//itemService.addEventListener(ResultEvent.RESULT,buyItemResultEvent);
			//packageService = new UserPackageFlexService();
			//packageService.addEventListener(RemoteExceptionEvent.REMOTE_EXCEPTION,exceptionEvent);
			//packageService.addEventListener(UserPackageFlexService.ADAPT_ITEM_FUN,adaptResultEvent);
			//packageService.addEventListener(UserPackageFlexService.SPLIT_TITEM_FUN,splitResultEvent);
			//packageService.addEventListener(UserPackageFlexService.MERGE_ITEM_FUN,mergeResultEvent);
		}
		
		public function buyItem(itemId:String,count:int,position:int):void{
			//itemService.buyItem(userID,itemId,count,position);
		}
		
		public function adaptItemPosition(oldpos:int,newpos:int,count:int):void{
			//packageService.adaptItemPosition(userID,oldpos,newpos,count);
		}
		
		public function splitItems(oldpos:int,newpos:int,count:int):void{
			//packageService.splitItems(userID,oldpos,newpos,count);
		}
		
		public function mergeItems(oldpos:int,newpos:int,count:int):void{
			//packageService.mergeItems(userID,oldpos,newpos,count);
		}
		
		/**
		 * 包裹使用功能执行
		 * @param type
		 * @param id
		 *
		 */
		public function itemUsing(prop:ItemViewModel):void
		{
			var type:int = prop.dataModel.itemGenre;
			MyLog.debug("this prop is card..." + type);
			var message:Object = null;
			switch(type)
			{
				case ItemDataModel.CARD_TYPE:
					//GameHallHelper.getInstance().dispatch("usingItem",prop.dataModel);
					return ;
				case ItemDataModel.APPEARANCE_TYPE:
					return ;
				default:
					return ;
			}
		}
		
		public function clearAll():void{
			/* itemService.removeEventListener(RemoteExceptionEvent.REMOTE_EXCEPTION,exceptionEvent);
			itemService.removeEventListener(ResultEvent.RESULT,buyItemResultEvent);
			packageService.removeEventListener(RemoteExceptionEvent.REMOTE_EXCEPTION,exceptionEvent);
			packageService.removeEventListener(UserPackageFlexService.ADAPT_ITEM_FUN,adaptResultEvent);
			packageService.removeEventListener(UserPackageFlexService.SPLIT_TITEM_FUN,splitResultEvent);
			packageService.removeEventListener(UserPackageFlexService.MERGE_ITEM_FUN,mergeResultEvent);
			packageService.clearAll();
			itemService.clearAll();
			packageService = null;
			itemService = null;
			userID = null;
			itemsContainer = null; */
		}
		/**
		 * 购买的请求结果具体处理方法 
		 * @param itemData
		 * 
		 */		
		/* private function resultBought(itemData:ItemDataModel):void{
			var validatePanel:ValidateBuyPanel=UIManager.getInstance().getPanelByName(ValidateBuyPanel.NAME) as ValidateBuyPanel;
			if (validatePanel)
			{
				UIManager.getInstance().removedFromDesktop(validatePanel);
			}

			if (itemData.result)
			{

				var temp:ItemViewModel=ItemHelper.cloneProp(itemData, false);
				temp.loadIcon();
				var index:int=itemData.currentPosistion;
				if (index < 35)
				{
					itemsContainer.addProp(temp, index);
					itemsContainer.dispatchEvent(new SelectionEvent(SelectionEvent.BUY_PROP, itemData.priceCityCoin.toString(),itemData.priceGoldCoin.toString()));
				}
			}
			else
			{
				Alert.show(itemData.message, "错误");
			}
		} */
		/**
		 * 购买的请求结果处理事件 
		 * @param itemData
		 * 
		 */	
		private function buyItemResultEvent(event:ResultEvent):void{
			if(event.result){
				var itemdata:ItemDataModel = event.result as ItemDataModel;
				//resultBought(itemdata);
			}else{
				//UIManager.getInstance().removedFromDesktopByName(ValidateBuyPanel.NAME);
				Alert.show("购买道具数据异常!","错误");
			}
		}
		/**
		 * 道具移动一个格子的结果处理事件 
		 * @param event
		 * 
		 */		
		private function adaptResultEvent(event:ResultEvent):void{
			if(event.result){
				var obj:ObjectProxy = event.result as ObjectProxy;
				if(obj.result){
					var mapobj:Object = obj.map;
					var olditemData:ItemDataModel = mapobj.oldPosition as ItemDataModel;
					var newitemData:ItemDataModel = mapobj.newPosition as ItemDataModel;
					itemsContainer.adaptItemPosition(olditemData,newitemData);
				}else{
					Alert.show(obj.message,"错误");
				}
				
			}else{
				Alert.show("移动道具数据异常!","错误");
			}
		}
		/**
		 * 合并道具的结果事件 
		 * @param event
		 * 
		 */		
		private function mergeResultEvent(event:ResultEvent):void{
			if(event.result){
				var obj:ObjectProxy = event.result as ObjectProxy;
				if(obj.result){
					var mapobj:Object = obj.map;
					var olditemData:ItemDataModel = mapobj.oldPosition as ItemDataModel;
					var newitemData:ItemDataModel = mapobj.newPosition as ItemDataModel;
					itemsContainer.mergeItems(olditemData,newitemData);
				}else{
					Alert.show(obj.message,"错误");
				}
				
			}else{
				Alert.show("移动道具数据异常!","错误");
			}
		}
		/**
		 * 拆分道具的结果事件
		 * @param event
		 * 
		 */		
		private function splitResultEvent(event:ResultEvent):void{
			if(event.result){
				var obj:ObjectProxy = event.result as ObjectProxy;
				if(obj.result){
					var mapobj:Object = obj.map;
					var olditemData:ItemDataModel = mapobj.oldPosition as ItemDataModel;
					var newitemData:ItemDataModel = mapobj.newPosition as ItemDataModel;
					itemsContainer.splitItemGroup(olditemData,newitemData);
				}else{
					Alert.show(obj.message,"错误");
				}
				
			}else{
				Alert.show("移动道具数据异常!","错误");
			}
		}
		
		/* private function exceptionEvent(event:RemoteExceptionEvent):void{
			Alert.show(event.message,"错误");
			UIManager.getInstance().removedFromDesktopByName(ValidateBuyPanel.NAME);
			UIManager.getInstance().removedFromDesktopByName(PackagePanel.NAME);
		} */
		
		/**
		 * 克隆一个道具
		 * @param id
		 * @param model
		 * @return
		 *
		 */
		public static function cloneProp(model:ItemDataModel,isSystem:Boolean,icon:BitmapData = null):ItemViewModel
		{
			var prop:ItemViewModel = new ItemViewModel();

			if (icon)
			{
				prop.icon = icon;
			}
			prop.dataModel.isSystem = isSystem;
			prop.dataModel.packageId = model.packageId;
			prop.dataModel.id = model.id;
			prop.dataModel.name = model.name;
			prop.dataModel.circulation = model.circulation;
			prop.dataModel.circulationMax = model.circulationMax;
			prop.dataModel.itemType = model.itemType;
			prop.dataModel.tip = model.tip;
			prop.dataModel.capability = model.currentCapability;
			prop.dataModel.exchangeFlag = model.exchangeFlag;
			prop.dataModel.target = model.target;
			prop.dataModel.restrict = model.restrict;
			prop.dataModel.priceCityCoin = model.priceCityCoin;
			prop.dataModel.priceGoldCoin = model.priceGoldCoin;
			prop.dataModel.capabilityMax = model.capabilityMax;
			prop.dataModel.iconPath = model.iconPath;
			prop.dataModel.position = model.currentPosistion;
			prop.dataModel.description = model.description;
			prop.dataModel.duration = model.duration;
			prop.dataModel.status = model.status;
			prop.dataModel.color = model.color;
			return prop;
		}
	}
}